<template>
  <a-row :gutter="24" >
    <a-col :md="4" :sm="24">
      <a-card :bordered="false" :loading="treeLoading">
        <div v-if="this.orgTree !== ''">
          <a-tree
            :treeData="orgTree"
            v-if="orgTree.length"
            @select="handleClick"
            :defaultExpandAll="true"
            :replaceFields="replaceFields" />
        </div>
        <div v-else>
          <a-empty :image="simpleImage" />
        </div>
      </a-card>
    </a-col>
    <a-col :md="19" :sm="24">
      <a-card :bordered="false">
        <!--<div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">

              &lt;!&ndash;<a-col :md="8" :sm="24">
                <a-form-item label="所属组织">
                  <a-input v-model="queryParam.name" placeholder="请输入所属组织"/>
                </a-form-item>
              </a-col>&ndash;&gt;

              &lt;!&ndash;<a-col :md="!advanced && 8 || 24" :sm="24">
                <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                  <a-button type="primary" class="btn_search_reset" @click="$refs.table.refresh(true)"><a-icon type="search" />查询</a-button>
                  <a-button style="margin-left: 8px" class="btn_search_reset" @click="() => queryParam = {}"><a-icon type="reload" />重置</a-button>
                </span>
              </a-col>&ndash;&gt;

            </a-row>
          </a-form>
        </div>
-->
        <div class="table-operator" style="text-align: right;padding-right: 10px;">
          <a-button type="primary" class="btn_add" @click="$refs.addForm.add()" icon="plus">新增</a-button>
        </div>
        <!--        2022/12/15删除多选框-->
        <s-table
          ref="table"
          size="default"
          :columns="columns"
          :data="loadData"
          :alert="true"
        >

          <span slot="action" slot-scope="text, record">
            <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>
            <a-divider type="vertical"/>
            <a-dropdown>
              <a class="btn_more"> 更多 <a-icon type="down"/></a>
              <a-menu slot="overlay">
                <a-menu-item>
                  <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">
                    <a>删除</a>
                  </a-popconfirm>
                </a-menu-item>
              </a-menu>
            </a-dropdown>
          </span>
        </s-table>

        <add-form ref="addForm" @ok="handleOk"/>
        <edit-form ref="editForm" @ok="handleOk"/>

      </a-card>
    </a-col>
  </a-row>
</template>

<script>
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/thirdpartyinterconnection/thirdPartyInterconnectionManage'
  import addForm from './addForm'
  import editForm from './editForm'
  import { Empty } from 'ant-design-vue'
  import { getOrgTree } from '@/api/modular/system/orgManage'

  export default {
    components: {
      STable,
      addForm,
      editForm
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '第三方接口地址',
            dataIndex: 'thirdPartyUrl'
          },
          {
            title: 'appKey',
            dataIndex: 'appKey'
          },
          {
            title: '业务系统名称',
            dataIndex: 'businessSystemName'
          },
          /* {
            title: '所属组织',
            dataIndex: 'orgId'
          }, */
          {
            title: '时间',
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        replaceFields: {
          key: 'id'
        },
        orgTree: [],
        simpleImage: Empty.PRESENTED_IMAGE_SIMPLE,
        treeLoading: true
      }
    },

    created () {
      /**
       * 获取到机构树，展开顶级下树节点，考虑到后期数据量变大，不建议全部展开
       */
      getOrgTree(Object.assign(this.queryParam)).then(res => {
        this.treeLoading = false
        if (!res.success) {
          return
        }
        this.orgTree = res.data
      })
    },

    methods: {
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      /**
       * 点击左侧机构树查询列表
       */
      handleClick (e) {
        this.queryParam.orgId = e.toString()
        this.$refs.table.refresh()
      }
    }
  }
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
